package com.qf.zyh.dao.Impl;

import com.qf.zyh.dao.AddressDao;
import com.qf.zyh.entity.Address;
import com.qf.zyh.util.DBUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class AddressDaoImpl implements AddressDao {
    private QueryRunner qr = new QueryRunner();

    @Override
    public List<Address> seleAddr(int uid) {
        try {
            return  qr.query(DBUtil.getConnection(),"select aid,uid,aname,aphone,adetail,astate from address where uid=? ORDER BY astate DESC",new BeanListHandler<>(Address.class),uid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int addAddr(Address ar) {
        try {
            return qr.update(DBUtil.getConnection(),"insert into address values(default,?,?,?,?,?)",ar.getUid(),ar.getAname(),ar.getAphone(),ar.getAdetail(),ar.getAstate());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int deleAddr(Integer aid) {
        try {
            return qr.update(DBUtil.getConnection(),"delete from address where aid=?",aid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int updastate(Integer aid) {
        try {
            return qr.update(DBUtil.getConnection(),"update address set astate=1 where aid=?",aid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }


    @Override
    public int updastatee(Integer aid, int uid) {
        try {
            return qr.update(DBUtil.getConnection(),"update address set astate=0 where uid = ? and aid !=?",uid,aid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int updateaddr(Address a) {
        try {
            return qr.update(DBUtil.getConnection(),"update address set aname=?,aphone=?,adetail=? where aid=?",a.getAname(),a.getAphone(),a.getAdetail(),a.getAid());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
